package com.fancy.learncenter.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.andview.refreshview.XRefreshView;
import com.fancy.learncenter.adapter.FancyActivityAdapter;
import com.fancy.learncenter.adapter.TrainApplyAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.TrainApplyListDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.view.CustomFootView;
import com.fancy.learncenter.view.CustomHeaderView;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hyy on 2018/5/5.
 */

public class MyHdApplyFragment extends Fragment {


    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;
    @Bind(R.id.prompt_img)
    ImageView promptImg;

    public MyHdApplyFragment() {
        // Required empty public constructor
    }

    FancyActivityAdapter trainVideoAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_train_apply, container, false);
        ButterKnife.bind(this, view);


        ArrayList<TrainApplyListDataBean> itemDatas = new ArrayList<>();
        trainVideoAdapter = new FancyActivityAdapter(getActivity(), itemDatas);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        recycleView.setAdapter(trainVideoAdapter);
        trainVideoAdapter.setCustomLoadMoreView(new CustomFootView(getActivity()));
        xRefreshView.setPullLoadEnable(true);
        xRefreshView.setCustomHeaderView(new CustomHeaderView(getActivity()));
        xRefreshView.setPinnedTime(1000);
        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
                LogUtil.MyLog("onLoadMore", "====onRefresh==");
                getDatas(true);
            }

            @Override
            public void onRefresh(boolean isPullDown) {

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                getDatas(false);
                LogUtil.MyLog("onLoadMore", "====onLoadMore==");
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double headerMovePercent, int offsetY) {

            }
        });

        getDatas(true);

        return view;
    }


    public static TrainApplyFragment newInstance(String isMy) {

        Bundle args = new Bundle();
        args.putString("isMy", isMy);
        TrainApplyFragment fragment = new TrainApplyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    String isMy = "";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            isMy = getArguments().getString("isMy");
        }
    }


    int page = 0;

    private void getDatas(final boolean isRefresh) {
        Map map = new HashMap();

        if (isRefresh) {
            page = 0;
        } else {
            page++;
        }
        if (!TextUtils.isEmpty(isMy)) {
            map.put("token", SPUtils.getToken());
            map.put("userId", SPUtils.getUserID());
        }
        HttpMehtod.getInstance().myHdApplyList(map, new IdeaObserver<BaseDataBean<List<TrainApplyListDataBean>>>() {

            @Override
            public void onSuccess(BaseDataBean<List<TrainApplyListDataBean>> listBaseDataBean) {
                trainVideoAdapter.notifyDataSetChanged((ArrayList<TrainApplyListDataBean>) listBaseDataBean.getData());
                if (isRefresh) {
                    xRefreshView.stopRefresh();
                    xRefreshView.setLoadComplete(false);
                    if (listBaseDataBean.getData() == null) {

                        promptImg.setVisibility(View.VISIBLE);
                        trainVideoAdapter.notifyDataSetChanged(new ArrayList<TrainApplyListDataBean>());
                        return;
                    }

                    promptImg.setVisibility(View.GONE);
                    trainVideoAdapter.notifyDataSetChanged((ArrayList<TrainApplyListDataBean>) listBaseDataBean.getData());
                } else {
                    if (listBaseDataBean.getData() == null) {
                        xRefreshView.setLoadComplete(true);
                    } else if (listBaseDataBean.getData() != null && listBaseDataBean.getData().size() < Constant.PAGE_NUM) {
                        xRefreshView.setLoadComplete(true);
                    } else {
                        xRefreshView.stopLoadMore();
                    }


                    if (listBaseDataBean.getData() == null) {
                        return;
                    }
                    trainVideoAdapter.addAll(listBaseDataBean.getData());
                }


            }

            @Override
            public void onFail(int errorCode) {
                xRefreshView.stopRefresh();
                xRefreshView.setLoadComplete(false);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
